# Definition for a binary tree node.
from typing import Optional


class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution:
    def isUnivalTree(self, root: Optional[TreeNode]) -> bool:
        def equal(root: Optional[TreeNode], val: int, hint: bool) -> bool:
            if not hint:
                return False
            if not root:
                return True
            if root.val != val:
                return False
            hint = root.val == val
            return equal(root.left, val, hint) and equal(root.right, val, hint)
        return equal(root, root.val, True)